//
//  YZMCommissionViewController.m
//  YZMicroStock
//
//  Created by apple on 17/4/1.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "YZMCommissionViewController.h"
#import "YZMDayGainComCell.h"
#import "YZMDayMonthHeaderView.h"
#import "YZClient.h"
#import "YZMUserGainModel.h"

static NSString *const sYZMDayGainCellID = @"YZMDayGainComCell";
static NSString *const sYZMDayComHeaderViewID = @"YZMDayMonthHeaderView";

@interface YZMCommissionViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;


@end

@implementation YZMCommissionViewController

-(NSMutableArray *)dataSource{
    
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed=YES;
        self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
        self.title = @"佣金收入榜";
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self createObj];
    [self fetchCommissionList];
    // Do any additional setup after loading the view.
}

- (void)createObj{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 1) style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [YZMAppStyle appBackgroundColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[YZMDayGainComCell class] forCellReuseIdentifier:sYZMDayGainCellID];
    [_tableView registerClass:[YZMDayMonthHeaderView class] forHeaderFooterViewReuseIdentifier:sYZMDayComHeaderViewID];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
    
}

#pragma warming tableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YZMDayGainComCell *Cell = [tableView dequeueReusableCellWithIdentifier:sYZMDayGainCellID forIndexPath:indexPath];
    
    YZMIndexCount indexCount;
    indexCount.index = indexPath.row;
    indexCount.count = _dataSource.count;
    Cell.indexCount = indexCount;
    
    Cell.indexForImgageView = indexPath.row;
    YZMUserGainModel *model = _dataSource[indexPath.row];
    Cell.gainModel = model;
    
    return Cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 5;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    YZMDayMonthHeaderView *headerView=[tableView dequeueReusableHeaderFooterViewWithIdentifier:sYZMDayComHeaderViewID];
    headerView.headStyle = HEADSTYLE_CONMISION;
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 58;
}

- (void)fetchCommissionList{
    
    [YZClient rank_FetchUnionRanksuccess:^(NSURLSessionDataTask *task, id responseObject) {
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            NSArray *array = topModel.data;
            [self.dataSource addObjectsFromArray:array];
            [_tableView reloadData];
        }else{
            
            [topModel showMessage];
        }
    } failure:^(NSError *error) {
        [error yz_showMessage];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
